package com.fau.cm.action.index;


import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallTerminate;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  话务管理已终止任务Action
 * @author yangzj
 * @datetime 2014年10月14日,上午10:39:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallManageTerminateAction extends PublicAction implements ModelDriven<MedicalCallTerminate>{
	
	private static final long serialVersionUID = 6349279192012513543L;
	
	private MedicalCallTerminate callTerminate = new MedicalCallTerminate();
	
	/**
	 * @desc 跳转话务任务管理--已结束页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:45:23
	 */
	@Action(value = "call_manage_terminate_init", results = {@Result(location = "call_manage_terminate_init.jsp")})
	public String init(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午4:46:43
	 */
	@Action(value = "call_manage_terminate_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallManagerService().findCallTerminateByPage(callTerminate, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_manage_callTerminate_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callTerminate.getId()) && StringUtils.isNotBlank(callTerminate.getCategory())){
			this.baseAllService.getCallManagerService().editCallTerminateCategory(callTerminate.getId(), callTerminate.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_manage_Terminate_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callTerminate.getId()) && StringUtils.isNotBlank(callTerminate.getPocessingStatus())) {
			this.baseAllService.getCallManagerService().editCallTerminateSta(callTerminate.getId(), callTerminate.getPocessingStatus());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_manage_terminate_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callTerminate.getId())){
			this.baseAllService.getCallManagerService().delTerminateCall(callTerminate);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出所有-已终止
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午8:00:46
	 */
	@Action(value = "call_manage_terminate_export_all")
	public String terminateExportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已终止话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallManagerService().callManageTerminateExportAll(admin,this.callTerminate);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-已终止
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午8:01:11
	 */
	@Action(value = "call_manage_terminate_export_cur")
	public String terminateExportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"已终止话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(callTerminate.getId())){
				Workbook workbook = this.baseAllService.getCallManagerService().callManageTerminateExportCurrent(callTerminate.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalCallTerminate getModel() {
		return callTerminate;
	}
	

}
